import json

from django.core.cache import cache


def get_ffk3_piece_trend_data_range(group, _type, gte=10, lte=100, now=True):
    res = []
    data = cache.get(f'TREND:FFK3:PRED:{group // 100 + 1}:{group % 100 + 1}:{_type}:LIST')
    data = data or '[]'
    data = json.loads(data)
    for row in data:
        if gte <= row['conn'] <= lte:
            if not now or row.get('now'):
                res.append({
                    'group': group % 100 + 1,
                    'page': group // 100 + 1,
                    'type': _type,
                    **row,
                })
    return res


def get_ffk3_trend_data_range(gte=10, lte=100, now=True):
    types = ['ds', 'dx']
    res = []
    for group in range(1000):
        for _type in types:
            res += get_ffk3_piece_trend_data_range(group, _type, gte, lte, now)
    return res